<?php

namespace app\modules\admin\controllers;

use app\common\services\StaticServices;
use app\models\User;
use Yii;
use yii\base\Exception;
use yii\web\Controller;
use yii\web\Response;

class MediationController extends Controller
{
    public function actionIndex()
    {
        $p = Yii::$app->request->get('p', 1);
        if (Yii::$app->admin->id != 1) {
            $parent_info = User::find()->where(['user_name' => Yii::$app->admin->identity->admin_name, 'user_pwd' => Yii::$app->admin->identity->admin_pwd, 'role_pid' => 0])->one();

            $user_info = User::find()->where(['role_pid' => $parent_info->id, 'user_status' => 1]);
        } else {
            $user_info = User::find()->where(['<>', 'user_ID', '0'])->andWhere(['<>', 'user_phone', '1111'])->andWhere(['user_role' => 4, 'user_status' => 1]);
        }
        $page_size = StaticServices::$page;
        $total_count = $user_info->count();
        $total_page = ceil($total_count / $page_size);

        $list = $user_info->offset(($p - 1) * $page_size)->orderBy(['update_time' => SORT_DESC])->limit($page_size)->all();

        return $this->render('index', ['list' => $list, 'p' => $p, 'total_page' => $total_page]);
    }

    public function actionSet()
    {
        $user_id = intval(Yii::$app->request->get('id'));
        $parent_info = User::find()->where(['user_name' => Yii::$app->admin->identity->admin_name, 'user_pwd' => Yii::$app->admin->identity->admin_pwd, 'role_pid' => 0])->one();
        $user_info = [];
        if ($user_id) {
            $user_info = User::find()->where(['id' => $user_id])->one();
        }

        if (Yii::$app->request->isAjax) {
            Yii::$app->response->format = Response::FORMAT_JSON;
            try {
                $user_name = trim(Yii::$app->request->post('user_name'));
                $user_phone = trim(Yii::$app->request->post('user_phone'));
                $user_ID = trim(Yii::$app->request->post('user_ID'));
                $user_company = trim(Yii::$app->request->post('user_company'));
                $user_id = intval(Yii::$app->request->post('user_id'));

                if (mb_strlen($user_name, 'utf-8') < 2) {
                    throw new Exception('请填写正确的用户名称');
                }
                if (!preg_match("/^1[34578]{1}\d{9}$/", $user_phone)) {
                    throw new Exception('请填写正确的用户手机号码');
                }
                if (!StaticServices::is_idcard($user_ID)) {
                    throw new Exception('请填写正确的用户身份号');
                }

                if (mb_strlen($user_company, 'utf-8') < 2) {
                    throw new Exception('请填写正确的用户单位');
                }

                $user = User::find()->where(['user_ID' => $user_ID])->one();
                if ($user) {
                    throw new Exception('此用户已经存在');
                }

                $user_arr = User::find()->where(['id' => $user_id])->one();
                if ($user_arr) {
                    $user_info = $user_arr;
                    $user_info->user_name = $user_name;
                    $user_info->user_phone = $user_phone;
                    $user_info->user_ID = $user_ID;
                    $user_info->user_role = 4;
                    $user_info->role_pid = $parent_info->id;
                    $user_info->user_company = $user_company;
                    $user_info->update_time = time();
                    $user_info->save(0);
                    return ['code' => 200, 'msg' => '操作成功'];
                } else {
                    $default_pwd = strtolower(StaticServices::getRandChar(6));
                    $user_info = new User();
                    $user_info->create_time = time();
                    $user_info->user_name = $user_name;
                    $user_info->user_phone = $user_phone;
                    $user_info->user_ID = $user_ID;
                    $user_info->user_pwd = $default_pwd;
                    $user_info->user_role = 4;
                    $user_info->role_pid = $parent_info->id;
                    $user_info->user_company = $user_company;
                    $user_info->update_time = time();
                    $user_info->create_time = time();
                    if ($user_info->save(0)) {
                        //发送短信给中介人
                        $template = '欢迎使用全民推荐平台。登录账号为您的手机号，初始密码为{code}，密码可在设置页面修改；如非本人操作，请忽略此短信';
                        StaticServices::getCode($user_info->user_pwd, $template, $user_info->user_phone, 2);
                    }
                    return ['code' => 200, 'msg' => '操作成功'];
                }
            } catch (Exception $e) {
                return ['code' => -200, 'msg' => $e->getMessage()];
            }
        }

        return $this->render('set', ['info' => $user_info]);
    }

    public function actionOps()
    {
        if (Yii::$app->request->isAjax) {
            Yii::$app->response->format = Response::FORMAT_JSON;
            try {
                $user_id = intval(Yii::$app->request->post('user_id'));
                if (!$user_id) {
                    throw new Exception('参数有误');
                }
                $user_info = User::find()->where(['id' => $user_id])->one();
                if (!$user_info) {
                    throw new Exception('出错了');
                }
                $user_info->user_status = 0;
                $user_info->update_time = time();
                $user_info->save(0);
                return ['code' => 200, 'msg' => '操作成功'];
            } catch (Exception $e) {
                return ['code' => -200, 'msg' => $e->getMessage()];
            }
        }
    }
}